package com.chpro.scsw.dao.impl;



import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.chpro.scsw.dao.BaseDao;
import lombok.extern.log4j.Log4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

/**
 * Dao基类实现类封装CRUD方法
 * Title: 泛型DaoImpl
 * Description:
 * Company: 上海理想信息产业(集团)有限公司
 * @author 
 * create date : 2014-04-15
 * @version 1.0
 */
@Repository(value="baseDao")
@Log4j
public class BaseDaoImpl<T> implements BaseDao<T> {

	@Resource(name="sqlSessionTemplate")
	private SqlSessionTemplate sqlSessionTemplate;
	
	/**
	 * 增加
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 封装数据的实体
	 * @return 返回操作结果
	 * @throws Exception 抛出所有异常
	 */
	public boolean add(String classMethod, T entity) throws Exception {
		log.info("dao add!"+classMethod);
		boolean flag = false;
		try {
			flag = sqlSessionTemplate.insert(classMethod, entity) > 0 ? true : false;
		} catch (Exception e) {
			flag = false;
			log.error("dao add:",e);
			throw e;
		}
		return flag;
	}

	/**
	 * 修改
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 封装数据的实体
	 * @return 返回操作结果
	 * @throws Exception 抛出所有异常
	 */
	public boolean edit(String classMethod, T entity) throws Exception {
		log.info("dao edit!"+classMethod);
		boolean flag = false;
		try {
			flag = sqlSessionTemplate.update(classMethod, entity) > 0 ? true : false;
		} catch (Exception e) {
			flag = false;
			log.error("dao edit:",e);
			throw e;
		}
		return flag;
	}
	
	/**
	 * 以id为条件查找对象
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 封装数据的实体
	 * @return 返回查询结果
	 * @throws Exception 抛出所有异常
	 */
	@SuppressWarnings("unchecked")
	public T get(String classMethod, T entity) throws Exception {
		log.info("dao get!"+classMethod);
		T result = null;
		try {
			result = (T) sqlSessionTemplate.selectOne(classMethod, entity);
		} catch (Exception e) {
			log.error("dao get:",e);
			throw e;
		}
		return result;
	}

	/**
	 * 删除
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 封装数据的实体
	 * @return 返回操作结果
	 * @throws Exception 抛出所有异常
	 */
	public boolean remove(String classMethod, T entity) throws Exception {
		log.info("dao remvoe!"+classMethod);
		boolean flag = false;
		try {
			flag = sqlSessionTemplate.delete(classMethod, entity) > 0 ? true : false;
		} catch (Exception e) {
			flag = false;
			log.error("dao remvoe:",e);
			throw e;
		}
		return flag;
	}
	
	/**
	 * 批量删除
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param list 要删除的数据集合
	 * @return 返回操作结果
	 * @throws Exception 抛出所有异常
	 */
	public boolean batchDelete(String classMethod,List<String>list) throws Exception{
		log.info("dao batchDelete!"+classMethod);
		boolean flag = false;
		try {
			flag = sqlSessionTemplate.delete(classMethod,list) > 0 ? true : false;
		} catch (Exception e) {
			flag = false;
			log.error("dao batchDelete:",e);
			throw e;
		}
		log.debug("flag:" + flag);
		return flag;
	}
	
	
	/**
	 * 批量增加
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param list 要增加的数据集合
	 * @return 返回操作结果
	 * @throws Exception  抛出所有异常
	 */
	public boolean batchAdd(String classMethod,List<T>list) throws Exception{
		log.info("dao batchAdd!"+classMethod);
		boolean flag = false;
		try {
			for (T t : list) {
				flag = sqlSessionTemplate.insert(classMethod,t) > 0 ? true : false;
			}
		} catch (Exception e) {
			flag = false;
			log.error("dao batchAdd:",e);
			throw e;
		}
		log.debug("flag:" + flag);
		return flag;
	}

	/**
	 * 查询
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @return 返回查询结果
	 * @throws Exception 抛出所有异常
	 */
	public List<T> getAllList(String classMethod) throws Exception {
		log.info("dao getAllList!"+classMethod);
		List<T> result = null;
		try {
			result = sqlSessionTemplate.selectList(classMethod);
			if(result != null){
				log.debug("result.size():" + result.size());
			}
		} catch (Exception e) {
			log.error("dao getAllList:",e);
			throw e;
		}
		return result;
	}
	
	/**
	 * 查询
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 封装数据的实体
	 * @return 返回查询结果
	 * @throws Exception 抛出所有异常
	 */
	public List<T> getAllList(String classMethod, T entity) throws Exception {
		log.info("dao getAllList!"+classMethod);
		List<T> result = null;
		try {
			result = sqlSessionTemplate.selectList(classMethod,entity);
			if(result != null){
				log.debug("result.size():" + result.size());
			}
		} catch (Exception e) {
			log.error("dao getAllList:",e);
			throw e;
		}
		return result;
	}
	/**
	 * 查询 -单个String 查询所有 如in ids 
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param params 封装数据的实体
	 * @return 返回查询结果
	 * @throws Exception 抛出所有异常
	 */
	public List<T> getAllListByIds(String classMethod, List<String> params) throws Exception {
		log.info("dao getAllList!"+classMethod);
		List<T> result=null;
		try {
			result = sqlSessionTemplate.selectList(classMethod,params);
		} catch (Exception e) {
			log.error("dao getAllList:",e);
			throw e;
		}
		return result;
	}
	
	/**
	 * 查询数量
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 封装数据的实体
	 * @return 返回查询结果
	 * @throws Exception 抛出所有异常
	 */
	public int getCount(String classMethod, T entity) throws Exception {
		log.info("dao getCount!"+classMethod);
		int result = 0;
		try {
			result = (Integer) sqlSessionTemplate.selectOne(classMethod,entity);
			log.debug("result:" + result);
		} catch (Exception e) {
			log.error("dao getCount:",e);
			throw e;
		}
		return result;
	}
	

	
	
	/**
	 * 获取单个值 如sum、count等
	 * @param classMethod  classMethod	mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param entity 查询条件实体
	 * @return
	 * @throws Exception
	 */
	public String getOneResult(String classMethod,T entity) throws Exception{
		log.info("dao getOneResult!"+classMethod);
		String result = null;
		try {
			result = (String)sqlSessionTemplate.selectOne(classMethod,entity);
		} catch (Exception e) {
			log.error("dao getOneResult:",e);
			throw e;
		}
		log.debug("result:" + result);
		return result;
	}
	
	/**
	 * 获取map集合
	 * @param classMethod  classMethod	mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param map 查询条件map
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getMap(String classMethod,Map<String, Object> map) throws Exception {
		log.info("dao getMap!"+classMethod);
		try {
			sqlSessionTemplate.selectList(classMethod, map);
		} catch (Exception e) {
			log.error("dao getMap:",e);
			throw e;
		}
		return map;
	}
	/**
	 * 获取对象 
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param param String
	 * @return 返回查询结果
	 * @throws Exception 抛出所有异常
	 */
	public T get(String classMethod,String param) throws Exception{
		log.info("dao get!"+classMethod);
		T result = null;
		try {
			result = sqlSessionTemplate.selectOne(classMethod, param);
		} catch (Exception e) {
			log.error("dao get:",e);
			throw e;
		}
		return result;
	}
	/**
	 * 批量更新
	 * @param classMethod mybatis配置文件里面对应的命名空间+要执行的sql语句id
	 * @param maps 要更新的数据集合
	 * @return 返回操作结果
	 * @throws Exception 抛出所有异常
	 */
	public boolean bantchEdit(String classMethod,Map<String,Object> maps) throws Exception{
		log.info("dao edit!"+classMethod);
		boolean flag = false;
		try {
			flag = sqlSessionTemplate.update(classMethod, maps) > 0 ? true : false;
		} catch (Exception e) {
			flag = false;
			log.error("dao edit:",e);
			throw e;
		}
		log.debug("flag:" + flag);
		return flag;
	}

	public T getCountObject(String classMethod, T entity) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



}
